package com.qys.livesMall.security.controller;

import com.qys.livesMall.common.constants.SysConstants;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.security.model.login.Login;
import com.qys.livesMall.security.model.user.SysUserInfo;
import com.qys.livesMall.security.model.user.UserVO;
import com.qys.livesMall.security.service.SysLoginServer;
import com.qys.livesMall.security.service.SysPermissionService;
import com.qys.livesMall.system.source.domain.entity.SysMenu;
import com.qys.livesMall.system.source.domain.vo.RouterVo;
import com.qys.livesMall.system.source.service.ISysMenuService;
import com.qys.livesMall.user.domain.vo.login.LoginVO;
import com.qys.livesMall.user.domain.vo.user.SysUserVO;
import com.qys.livesMall.user.service.ISysRoleMenuService;
import com.qys.livesMall.user.service.ISysUserService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 后台登录
 * @date 2023/05/22 15:59
 */
@RestController
@RequestMapping("/web/auth")
public class SysLoginController {

    @Autowired
    private SysLoginServer sysLoginServer;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private ISysRoleMenuService sysRoleMenuService;

    @Autowired
    private ISysMenuService iSysMenuService;

    @Autowired
    private ISysUserService userService;

    /**
     * 登录
     *
     * @param login
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public R<LoginVO> login(@RequestBody Login login, HttpServletRequest request) {
        return sysLoginServer.login(login.getUsername(), login.getPassword(), request);
    }

    /**
     * 登出
     *
     * @param request
     * @return
     */
    @PostMapping("/loginOut")
    public R<LoginVO> loginOut(HttpServletRequest request) {
        String token = request.getHeader(SysConstants.AUTHORIZATION);
        return sysLoginServer.loginOut(token);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("/getInfo")
    public R<SysUserInfo> getInfo() {
        SysUserInfo userInfo=new SysUserInfo();
        SysUserVO user = userService.selectUserById(SecurityUtils.getUserId());
        UserVO userVO=new UserVO();
        BeanUtils.copyProperties(user,userVO);
        userInfo.setUser(userVO);
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user.getUserId());
        userInfo.setRoles(roles);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission();
        userInfo.setPermissions(permissions);
        return R.ok(userInfo);
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("/getRouters")
    public R<List<RouterVo>> getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = sysRoleMenuService.selectMenuTreeByUserId(userId);
        return R.ok(iSysMenuService.buildMenus(menus));
    }
}

